# UTILITY.PY - Utility functions
# HIT3046 AI for GAMES
# ------- Assignment 2
# Authors: Mike Blackney, 0906123
#          Alex Bruce, 5409438

import random

from defaults import *

# Common random functions:

def RandomRange( low, high ):
    # Return a random number in the range [low...high]
    return int( random.random( ) * (high - low + 1) ) + low

def RandomEdge( ):
    # Return a random point along the edge of the screen:
    edge = RandomRange(1, 4)
    if edge == 1:
        return 0, RandomRange( 0, MAP_SIZE[1] * MAP_SCALE - 1 )
    elif edge == 2:
        return RandomRange( 0, MAP_SIZE[0] * MAP_SCALE - 1 ), 0
    elif edge == 3:
        return MAP_SIZE[0] * MAP_SCALE - 1, RandomRange( 0, MAP_SIZE[1] * MAP_SCALE - 1 )
    else:
        return RandomRange( 0, MAP_SIZE[0] * MAP_SCALE - 1 ), MAP_SIZE[1] * MAP_SCALE - 1


# Unit conversion

def MapToScreen( location ):
    # Convert from map coords to screen coords:
    return location * MAP_SCALE + (MAP_SIZE[0] / 2, MAP_SIZE[1] / 2)

def ScreenToMap( location ):
    # Convert from screen coords to map coords:
    ret_loc = (location / MAP_SCALE).trunc( )
    if ret_loc.x < 0: ret_loc.x = 0
    elif ret_loc.x >= MAP_SIZE[0]: ret_loc.x = MAP_SIZE[0]-1
    if ret_loc.y < 0: ret_loc.y = 0
    elif ret_loc.y >= MAP_SIZE[1]: ret_loc.y = MAP_SIZE[1]-1
    return ret_loc


